<?php
/**
 * Created by PhpStorm.
 * User: moquan
 * Date: 2018/5/26
 * Time: 17:16
 */

namespace app\admin\controller;
use app\admin\model\AdminModel;
use app\admin\validate\User;
use app\lib\exception\ParamException;
use think\Db;
use think\facade\Request;

class Admin extends Base {

    public function _initialize() {
        parent::_initialize();
    }

    /**
     * 管理员登陆
     */
    public function login() {
        $result = AdminModel::doLogin();
        if( $result || session('admin') ){
            $this->redirect('admin/index/index');
        }else{
            return $this->fetch();
        }
    }

    /*
     * 退出
     * */
    public function signOut(){
        session("admin",null);
        $this->redirect('admin/admin/login');
    }

    /*
     * 用户
     * */
    public function user(){
        /*
         *  page:当前页,
     *                            path:url路径,
     *                            query:url额外参数,
     *                            fragment:url锚点,
     *                            var_page:分页变量,
     *                            list_rows:每页数量
     *                            type:分页类名
         * */
        $config = [
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
        ];
        $user = Db::name('admin_user')->where('status',1)->paginate($config);
        $page = $user->render();
//        print_r($page);
        $this->assign('user',$user);
        $this->assign('page',$page);
        if($this->request->isAjax()){
            return json(['res'=>$user,'page'=>$page]);
        }else{
            return $this->fetch();
        }
    }

    /*
     * 添加用户
     * */
    public function add(){

        // 保存数据
        if( $this->request->isPost() ){
            $param = ( new User() )->goChcek( 'add' );

            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('head_img');
            if( $file ){
                $uploads = 'public' . DS . 'uploads';
                $info = $file->validate(['ext'=>'jpg,png'])->move(ROOT_PATH . $uploads);
                if($info){
                    $param['head_img'] = config('base_url').'public'.DS.'uploads'.DS.$info->getSaveName();
                }else{
                    throw new ParamException([
                        'msg' => $file->getError()
                    ]);
                }
            }else{
                throw new ParamException([
                    'msg' => "请选择要上传的文件"
                ]);
            }

            $admin = AdminModel::create( $param );
            if( !empty($admin) ){
                $this->success('操作成功',url('admin/admin/user'));
            }

            $this->error('操作失败');
            Ureturn ;
        }

        return $this->fetch();
    }

    /*
     * 添加用户
     * */
    public function edit( $id = null ){
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if( $this->request->isPost() ){
            $param = ( new User() )->goChcek( 'edit' );

            // 获取表单上传文件 例如上传了001.jpg
            /*$file = request()->file('head_img');
            if( $file ){
                $uploads = 'public' . DS . 'uploads';
                $info = $file->validate(['ext'=>'jpg,png'])->move(ROOT_PATH . $uploads);
                if($info){
                    $param['head_img'] = config('base_url').'public'.DS.'uploads'.DS.$info->getSaveName();
                }else{
                    throw new ParamException([
                        'msg' => $file->getError()
                    ]);
                }
            }else{
                throw new ParamException([
                    'msg' => "请选择要上传的文件"
                ]);
            }

            $admin = AdminModel::create( $param );
            if( !empty($admin) ){
                $this->success('操作成功',url('admin/admin/user'));
            }

            $this->error('操作失败');*/
            return ;
        }

        // 获取数据
        $info = AdminModel::where('admin_id', $id)->field('password', true)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }












}